package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Exerciserecord;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.bo.Patient;
import com.jrtc.base.entity.vo.*;
import com.jrtc.service.ExerciserecordService;
import com.jrtc.service.PatientService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 患者运动记录表(Exerciserecord)表控制层
 *
 * @author makejava
 * @since 2022-03-21 17:19:03
 */
@RestController
@RequestMapping("exerciserecord")
public class ExerciserecordController {
    /**
     * 服务对象
     */
    @Resource
    private ExerciserecordService exerciserecordService;
    @Resource
    private PatientService patientService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Exerciserecord selectOne(Integer id) {
        return this.exerciserecordService.queryById(id);
    }
    
    @RequestMapping("getExerciserecordList")
    public ResultDTO getExerciserecordList(@RequestBody Exerciserecord exerciserecord)
    {
        IPage<Exerciserecord> ipage = exerciserecordService.queryExerciserecordList(exerciserecord);
        return ResultDTOBuilder.success(ipage);
    }

    @RequestMapping("insertExerciserecord")
    public ResultDTO insertExerciserecord(@RequestBody Exerciserecord exerciserecord)
    {
        Exerciserecord tmpExerciserecord = exerciserecordService.insert(exerciserecord);
        if (tmpExerciserecord != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getExerciserecordById")
    public ResultDTO getExerciserecordById(@RequestBody Map<String,Integer> map)
    {
        int exerciserecordId = map.get("exerciserecordId");
        Exerciserecord exerciserecord = exerciserecordService.queryById(exerciserecordId);
        if (exerciserecord==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(exerciserecord);
    }

    @RequestMapping("updateExerciserecord")
    public ResultDTO updateExerciserecord(@RequestBody Exerciserecord exerciserecord)
    {
        if (exerciserecord==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (exerciserecordService.update(exerciserecord)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deleteExerciserecord")
    public ResultDTO deleteExerciserecord(@RequestBody Map<String,Integer> map)
    {
        int exerciserecordId = map.get("exerciserecordId");
        if (exerciserecordService.deleteById(exerciserecordId))
        {
            return ResultDTOBuilder.success("删除成功！");
        }
        return ResultDTOBuilder.failureMsg("删除失败！");
    }

    /**
     * 患者运动记录
     * */
    @PostMapping("/selectExerciserecordList")
    public ResultDTO selectExerciserecordList(HttpServletRequest request, @RequestBody Map<String, Object> map){
        Integer patientId;
        if (map.get("id") != null && !map.get("id").equals("")){
            patientId = Integer.parseInt(map.get("id").toString());
        }
        else {
            Patient patient;
            try {
                patient = patientService.getLoginPatient(request);
            } catch (Exception e){
                return null;
            }
            if (patient == null){
                return ResultDTOBuilder.failureMsg("登录已过期,请重新登陆");
            }
            patientId = patient.getPatientId();
        }

        List<ExerciserecordVo> exerciserecordVoList;
        try {
            exerciserecordVoList = exerciserecordService.selectExerciserecordList(patientId);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }
        if (exerciserecordVoList != null && exerciserecordVoList.size() > 0){
            return ResultDTOBuilder.success(exerciserecordVoList);
        }
        else {
            return ResultDTOBuilder.failureMsg("未查到数据");
        }
    }

    /**
     * 患者训练记录
     * */
    @PostMapping("selectTrainingRecord")
    public ResultDTO selectTrainingRecord(HttpServletRequest request){
        Patient patient;
        try {
            patient = patientService.getLoginPatient(request);
        } catch (Exception e){
            return null;
        }

        TrainingRecordVo trainingRecordVo;
        try {
            trainingRecordVo = exerciserecordService.selectTrainingRecordVo(patient.getPatientId());
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (trainingRecordVo != null){
            return ResultDTOBuilder.success(trainingRecordVo);
        }
        else {
            return ResultDTOBuilder.failureMsg("未查到数据");
        }
    }

    /**
     * 添加报告
     * */
    @PostMapping("/insertReport")
    public ResultDTO insertReport(@RequestBody Exerciserecord exerciserecord){
        InsertReportVo insertReportVo;
        try {
            insertReportVo = exerciserecordService.insertReport(exerciserecord);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        return ResultDTOBuilder.success(insertReportVo);
    }

    /**
     * 治疗记录
     * */
    @PostMapping("/treatmentRecords")
    public ResultDTO treatmentRecords(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        List<TreatmentRecordsVo> treatmentRecordsVoList = exerciserecordService.treatmentRecords(id);
        return ResultDTOBuilder.success(treatmentRecordsVoList);
    }

    /**
     * 运动记录图表
     * */
    @PostMapping("/exerciseChart")
    public ResultDTO exerciseChart(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        ExerciseReportVo exerciseReportVo = exerciserecordService.exerciseChart(id);
        return ResultDTOBuilder.success(exerciseReportVo);
    }
}